var vm = new Vue({
    el: '#app',
    data: {
        tableData: [],//表格当前页数据
        pagesize: 20,//默认每页数据量
        storeMember: {}, //当前选择行数据
        currentPage: 1,//当前页码
        totalCount: 0,//默认数据总数
        multipleSelection: [],//多选数组
        q: {//搜索条件
            productionStatus: '',
            payStatus: '',
            beginCreatetime: '',
            endCreatetime: '',
            radioTime: 0,
            orderId:'',
            name:''
        },
        loading: true,
        title: '',
        radioProduct: '',
        payedMoney: '',
        alipayTatal: '',
        weixinTatal: '',
        cashTatal: '',
        unTatal: '',
    },
    created: function () {
        Vue.nextTick(function () {
            //载入数据
            vm.loadData(vm.currentPage, vm.pagesize);
        });
    },
    methods: {
        //从后台获取数据
        loadData: function (page, limit) {
            vm.loading=true
            var params = {
                page: page,
                limit: limit,
                productionStatus: vm.q.productionStatus,
                payStatus: vm.q.payStatus + '',
                beginCreatetime: vm.q.beginCreatetime ? vm.dateFormat(new Date(vm.q.beginCreatetime)) : '',
                endCreatetime: vm.q.endCreatetime ? vm.dateFormat(new Date(vm.q.endCreatetime)) : '',
                radioTime: vm.q.radioTime,
                orderId: vm.q.orderId,
                name: vm.q.name,
            }
            $.ajax({
                type: "POST",
                url: baseURL + 'mercharReport/orderinfo/twblist',
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function (r) {
                    if (r.code === 0) {
                        vm.loading=false
                        vm.tableData = r.page.list
                        vm.totalCount = r.page.totalCount
                        vm.payedMoney = r.userData.payedMoney
                        vm.alipayTatal = r.userData.alipayTatal
                        vm.weixinTatal = r.userData.weixinTatal
                        vm.cashTatal = r.userData.cashTatal
                        vm.unTatal = r.userData.unTatal
                    } else {
                        vm.loading=false
                        alert(r.msg);
                    }
                }
            });
        },
        //多选响应
        handleSelectionChange: function (val) {
            // vm.multipleSelection = val;
            // console.log(vm.multipleSelection)
        },
        //点击行响应
        handleclick: function (row, event, column) {
            // vm.storeMember = JSON.parse(JSON.stringify(row));//深克隆
        },
        //弹出详情框
        showDetail: function (row) {
            vm.title = "用户详情";
            vm.storeMember = row
            vm.dialogFormVisible = true
        },
        //弹出新增框
        add: function () {
            vm.title = "新增用户";
            vm.dialogFormVisible = true
            vm.storeMember = {hangsAccountCustomer: 0};
        },
        //弹出编辑框
        update: function (index, row) {
            vm.title = '编辑用户'
            vm.storeMember = JSON.parse(JSON.stringify(row));//深克隆
            vm.dialogFormVisible = true
        },
        saveOrUpdate: function (formName) {
            this.$refs[formName].validate(function (valid) {
                if (valid) {
                    if (!(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(vm.storeMember.mobile))) {
                        alert("手机号码格式不正确");
                        return false;
                    }
                    var url = vm.storeMember.sid == null ? "storeMember/storemember/twbsave" : "storeMember/storemember/update";
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(vm.storeMember),
                        success: function (r) {
                            if (r.code === 0) {
                                vm.$alert('操作成功!', '提示', {
                                    confirmButtonText: '确定',
                                    callback: function (action) {
                                        vm.loadData(vm.currentPage = 1, vm.pagesize);//刷新后回到首页
                                        vm.$refs[formName].resetFields();
                                        vm.dialogFormVisible = false
                                    }
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //删除恢复
        del: function (index, row) {
            vm.$alert(row.del === 0 ? '确定删除?' : '确定恢复?', '提示', {
                confirmButtonText: '确定',
                callback: function (action) {
                    $.ajax({
                        type: "POST",
                        url: "userinfo/delete",
                        contentType: "application/json",
                        data: JSON.stringify(row),
                        success: function (r) {
                            if (r.code === 0) {
                                vm.loadData(vm.currentPage, vm.pagesize);//刷新后页面在当前页
                                //vm.loadData(vm.currentPage=1, vm.pagesize);刷新后页面回到首页
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                }
            });
        },
        //搜索
        search: function () {
            vm.loadData(vm.currentPage, vm.pagesize);
        },
        //重置
        rest: function () {
            vm.q.orderId = ''
            vm.q.name = ''
            vm.q.beginCreatetime = ''
            vm.q.endCreatetime = ''
            vm.q.radioTime = 0
            vm.q.payStatus = ''
            vm.q.productionStatus = ''
            vm.radioProduct=''
            vm.loadData(vm.currentPage = 1, vm.pagesize);
        },
        //每页显示数据量变更
        handleSizeChange: function (val) {
            vm.pagesize = val;
            vm.loadData(vm.currentPage, vm.pagesize);
        },
        //页码变更
        handleCurrentChange: function (val) {
            vm.currentPage = val;
            vm.loadData(vm.currentPage, vm.pagesize);
        },
        merchantsSourceFormatter: function (val) {
            if (val.merchantsSource == 0) {
                return '商城'
            }
            else if (val.merchantsSource == 1) {
                return '图文宝'
            }
            else {
                return ''
            }
        },
        hangsAccountCustomerFormatter: function (val) {
            if (val.hangsAccountCustomer == 0) {
                return '否'
            }
            else if (val.hangsAccountCustomer == 1) {
                return '是'
            }
            else {
                return ''
            }
        },

        accountCustomer: function () {
            vm.q.hangsAccountCustomer = 1
            vm.q.merchantsSource = ''
            vm.search()
        },
        twbCustomer: function () {
            vm.q.merchantsSource = 1
            vm.q.hangsAccountCustomer = ''
            vm.search()
        },
        mallCustomer: function () {
            vm.q.merchantsSource = '0'
            vm.q.hangsAccountCustomer = ''
            vm.search()
        },


        changeProduct: function (val) {
            vm.q.productionStatus = val
            vm.search()
        },
        changePay: function (val) {
            vm.q.payStatus = val
            vm.search()
        },
        changeTime: function (val) {
            vm.q.radioTime = val
            vm.search()
        },
        /* 格式化日期yyyy-MM-dd HH:mm:ss */
        dateFormat(date) {
            var year = date.getFullYear();
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
            var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
            return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
        },
        exportExcel() {
            //导出excel
            vm.q.orderId = vm.q.orderId.trim();
            vm.q.name = vm.q.name.trim();
            vm.q.payStatus = vm.q.payStatus+'';
            vm.q.beginCreatetime= vm.q.beginCreatetime ? vm.dateFormat(new Date(vm.q.beginCreatetime)) : ''
            vm.q.endCreatetime= vm.q.endCreatetime ? vm.dateFormat(new Date(vm.q.endCreatetime)) : ''
            var data = encodeURIComponent(JSON.stringify(vm.q));
            window.location.href = baseURL + "mercharReport/orderinfo/twvExportExcel?postData=" + data;
        }
    }
});